package cn.xaut.杂货;

public class demo896 {

    // 提前终止
    public boolean isMonotonic3(int[] A) {

        boolean incr = false, desc = false;
        for (int i = 1; i < A.length; i++) {
            if (A[i - 1] < A[i])
                incr = true;
            if (A[i - 1] > A[i])
                desc = true;
            if (incr && desc)
                return false;
        }
        return true;
    }
    
    // 一次遍历
    public boolean isMonotonic2(int[] A) {
        
        boolean incr = true, desc = true;
        for (int i = 1; i < A.length; i++) {
            if (A[i - 1] < A[i])
                desc = false;
            if (A[i - 1] > A[i])
                incr = false;
        }
        
        return incr || desc;
    }
    
    // 两次遍历
    public boolean isMonotonic(int[] A) {

        boolean flag1 = true, flag2 = true;
        for (int i = 1; i < A.length; i++) {
            if (A[i] < A[i - 1])
                flag1 = false;
        }

        for (int i = 1; i < A.length; i++) {
            if (A[i] > A[i - 1])
                flag2 = false;
        }

        return flag1 || flag2;
    }

    public static void main(String[] args) {

        System.out.println(new demo896().isMonotonic3(new int[]{1, 2, 2, 3}));//true
        System.out.println(new demo896().isMonotonic3(new int[]{6, 5, 4, 4}));//true
        System.out.println(new demo896().isMonotonic3(new int[]{1, 3, 2}));//false
    }
}
